/*package com.gohi.main.wxapi.service.impl;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.gohi.main.util.Utils;
import com.gohi.main.util.global.GloblConfig;
import com.gohi.main.wxapi.MsgType;
import com.gohi.main.wxapi.service.WxCmsService;
import com.gohi.main.wxapi.vo.Article;
import com.gohi.main.wxapi.vo.MsgNews;
import com.gohi.main.wxapi.vo.MsgRequest;
import com.gohi.main.wxapi.vo.MsgResponseNews;
import com.gohi.main.wxapi.vo.MsgResponseText;

*//**
 * 微信 Cms 服务层实现类；
 * 通过此类，调用cms提供的服务；
 * @author brain
 *//*

@Service
public class WxCmsServiceImpl implements WxCmsService {
	//获取 MsgResponseText 对象
	public MsgResponseText getMsgResponseText(MsgRequest msgRequest,WxMsgbase msgText){
		if(msgText != null){
			MsgResponseText reponseText = new MsgResponseText();
			reponseText.setToUserName(msgRequest.getFromUserName());
			reponseText.setFromUserName(msgRequest.getToUserName());
			reponseText.setMsgType(MsgType.Text.toString());
			reponseText.setCreateTime(new Date().getTime());
			//临时处理下  图文没出来
			try {
				msgText.setTexts( new String(msgText.getTexts().getBytes("GBK"), "ISO8859_1"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			reponseText.setContent(msgText.getTexts());
		
			return reponseText;
		}else{
			return null;
		}
	}
	
	//获取 MsgResponseNews 对象
	public MsgResponseNews getMsgResponseNews(MsgRequest msgRequest,List<WxMsgnews> msgNews){
	if(msgNews != null && msgNews.size() > 0){
			MsgResponseNews responseNews = new MsgResponseNews();
			responseNews.setToUserName(msgRequest.getFromUserName());
			responseNews.setFromUserName(msgRequest.getToUserName());
			responseNews.setMsgType(MsgType.News.toString());
			responseNews.setCreateTime(new Date().getTime());
			
			responseNews.setArticleCount(msgNews.size());
			List<Article> articles = new ArrayList<Article>(msgNews.size());
			for(WxMsgnews n : msgNews){
				Article a = new Article();
				a.setTitle(n.getTitle());
				a.setPicUrl(GloblConfig.host_url+n.getPicPath());
				//System.err.println(GloblConfig.host_url+n.getPicPath());
				if(Utils.show(n.getUrl()).equals("")){
					//如果为空
					a.setUrl(GloblConfig.host_url+"news.do?newsid="+n.getId()+"&id="+n.getParid());
				}else{
					a.setUrl(n.getUrl());
				}
			
				
				a.setDescription(n.getBrief());
				articles.add(a);
			}
			responseNews.setArticles(articles);
			
			return responseNews;
		}else{
			return null;
		}

	}

	
	//获取微信公众账号的菜单
	@Override
	public String getMenus() {
		List<AccountMenu> menus = menuDao.listWxMenus();
		if(!CollectionUtils.isEmpty(menus)){
			List<AccountMenu> parentAM = new ArrayList<AccountMenu>();
			Map<Long,List<JSONObject>> subAm = new HashMap<Long,List<JSONObject>>();
			
			for(AccountMenu m : menus){
				if(m.getParentid() == 1L){//一级菜单
					parentAM.add(m);
				}else{//二级菜单
					if(subAm.get(m.getParentid()) == null){
						subAm.put(m.getParentid(), new ArrayList<JSONObject>());
					}
					List<JSONObject> tmpMenus = subAm.get(m.getParentid());
					tmpMenus.add(getMenuJSONObj(m));
					subAm.put(m.getParentid(), tmpMenus);
				}
			}
			
			JSONArray arr = new JSONArray();
			for(AccountMenu m : parentAM){
				if(subAm.get(m.getId()) != null){//有子菜单
					arr.add(getParentMenuJSONObj(m,subAm.get(m.getId())));
				}else{//没有子菜单
					arr.add(getMenuJSONObj(m));
				}
			}
			JSONObject root = new JSONObject();
			root.put("button", arr);
			return JSONObject.fromObject(root).toString();
		}
		return "error";
	}
	
	*//**
	 * 此方法是构建菜单对象的；构建菜单时，对于  key 的值可以任意定义；
	 * 当用户点击菜单时，会把key传递回来；对已处理就可以了
	 * @param menu
	 * @return
	 *//*
	private JSONObject getMenuJSONObj(AccountMenu menu){
		JSONObject obj = new JSONObject();
		obj.put("name", menu.getName());
		obj.put("type", menu.getMtype());
		if("click".equals(menu.getMtype())){//事件菜单
			if("fix".equals(menu.getEventType())){//fix 消息
				obj.put("key", "_fix_" + menu.getMsgId());//以 _fix_ 开头
			}else{
				if(StringUtils.isEmpty(menu.getInputcode())){//如果inputcode 为空，默认设置为 subscribe，以免创建菜单失败
					obj.put("key", "subscribe");
				}else{
					obj.put("key", menu.getInputcode());
				}
			}
		}else{//链接菜单-view
			obj.put("url", menu.getUrl());
		}
		return obj;
	}
	
	private JSONObject getParentMenuJSONObj(AccountMenu menu,List<JSONObject> subMenu){
		JSONObject obj = new JSONObject();
		obj.put("name", menu.getName());
		obj.put("sub_button", subMenu);
		return obj;
	}
	
	
	//保存用户信息到 cms 数据库中
	public void saveAccountFans(JSONObject jsonObj){
		String openId = jsonObj.getString("openid");
		AccountFans fans = fansService.getByOpenId(openId);
		if(fans == null){
			fans = new AccountFans();
		}
		fans.setSubscribeStatus(new Integer(jsonObj.getInt("subscribe")));// 关注状态（1是关注，0是未关注），未关注时获取不到其余信息
		fans.setOpenId(openId);// 用户的标识
		fans.setGender(jsonObj.getInt("sex"));// 用户的性别（1是男性，2是女性，0是未知）
		fans.setNickname(jsonObj.getString("nickname"));// 昵称
		fans.setLanguage(jsonObj.getString("language"));// 用户的语言，简体中文为zh_CN
		fans.setCity(jsonObj.getString("city"));// 用户所在城市
		fans.setProvince(jsonObj.getString("province"));// 用户所在省份
		fans.setCountry(jsonObj.getString("country"));// 用户所在国家
		fans.setHeadimgurl(jsonObj.getString("headimgurl"));// 用户头像
		fans.setSubscribeTime(jsonObj.getString("subscribe_time"));// 用户关注时间
		fans.setRemark(jsonObj.getString("remark"));
		if(fans.getId() != null){
			fansService.update(fans);
		}else{
			fansService.add(fans);
		}
		
		//业务处理
	}
}



*/